$().ready(function() {
	$("#dateNoteDate").val('');
	$('textarea').val('');
	$('#intNoteID').val('');
});
function get_checkuplog_dates(){
	var months = { "January":1, "February":2, "March":3, "April":4, "May":5, "June":6, 
					"July":7, "August":8, "September":9, "October":10, "November":11, "December":12 };		
	var month = $('#calCheckuplog>.ui-datepicker>.ui-datepicker-header>.ui-datepicker-title>.ui-datepicker-month').text();
	var year = $('#calCheckuplog>.ui-datepicker>.ui-datepicker-header>.ui-datepicker-title>.ui-datepicker-year').text();
	set_calendar_days(months[month],year);
	
}
function set_calendar_days(month,year){
	session_code = $.cookie('session_code');
	xfunc = "checkuplog/list_checklupog";
	data_sent = {
			'intUserID':session_code
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request		
			$('#calCheckuplog>.ui-datepicker>table>tbody>tr>td').each(function(kc,vc){
				$(this).attr('onclick','clear_calendar_checkuplog_box();');
			});				
			date = '';
			$('.checkuplog-list-active').removeClass('checkuplog-list-active');
			$(result.dateNoteDateDB).each(function(k,v){
				
				date = result.dateNoteDateDB[k].split('-');
				
				yearMonth = date[0]+'-'+date[1];
				if($.trim(year+'-'+month)==yearMonth){
					$('#calCheckuplog>.ui-datepicker>table>tbody>tr>td').each(function(kc,vc){
						var data_year = $(this).attr('data-year');
						var data_month = $(this).attr('data-month');
						if($.trim(data_year)==year){
							if($.trim(data_month*1+1)==month){
								if($.trim(date[2])==$.trim($(this).text())){
									var day = $(this).text();
									$(this).addClass('checkuplog-list-active');
//									$(this).attr('onclick','get_checkuplog("'+result.intID[k]+'");');
									$('.checkuplog-list-active a').css({
										'background':'#e36e89',
										'color': '#FFFFFF'
									});
									$(this).attr('onclick','get_calendar_list_dates("'+day+'","'+month+'","'+year+'");');
								} 
							}
						}
					});
				}
			});
		}
	});			
}
function get_calendar_list_dates(d, m, y){
	$('.wrapper-notes-calendar-checkuplog').fadeOut(500,function(){
		$('.list-calendar-checkuplog-wrap').fadeIn(500,function(){
			
		});
	});	
	if(m.length<2){
		m='0'+m;
	}
	if(d.length<2){
		d='0'+d;
	}	
	y = y[y.length-2]+y[y.length-1];
	var date_sel = m+'/'+d+'/'+y;
	var intUserID = $.cookie('session_code');
	var intActive = $('#intNoteID').val();
	xfunc = "checkuplog/list_checklupog";
	data_sent = {
			'intUserID':intUserID
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request
			
			html='<lu>';
			html+='<li class="rowt" id="rowt"><i class="col1">Date</i><i class="col2">Title</i></li>';
			var c = 1;
			tgle_class = 'row1';
			
				$(result.strNoteTitle).each(function(k,v){
					if($.trim(result.dateNoteDate[k])==date_sel){
						if(tgle_class == 'row2'){
							tgle_class = 'row1';
						} else {
							tgle_class = 'row2';
						}
						strActive='';
						if(intActive==result.intID[k]){
							strActive = 'checkuplog-list-active';
						}
						html+='<li id="calendar_checkuplog_list_'+result.intID[k]+'" class="'+tgle_class+' '+strActive+' row-checklupog-list">';
							html+='<a onclick="get_checkuplog('+result.intID[k]+')"><i class="col1">'+result.dateNoteDate[k]+'</i><i class="col2">'+result.strNoteTitle[k]+'</i></a>';
						html+='</li>';
						
						c++;
					}
				});
			
			html+='</lu>';
			$('.list-calendar-checkuplog').html(html);
		}
	});		
}
function clear_calendar_checkuplog_box(){
	$('#title-note-calendar-checkuplog').val('');
	$('#notes-calendar-checkuplog').val('');								
	$('#intNoteID').val(0);
	$('.result-message-box').html('');	
}
function clear_note_checkuplog_box(){
	$('#title-note-checkuplog').val('');
	$('#notes-checkuplog').val('');								
	$('#intNoteID').val(0);
	$('.result-message-box').html('');	
}
function checkuplog(){
	html="";
	html+='<div class="wrapper-icon-checkuplog">';
		html+='<div class="left-side">';
			html+='<div class="calendar-icon btn-calendar-checkuplog"></div>';
			html+='<i class="clearfix"></i>';
			html+='<div class="calendar-text btn-calendar-checkuplog">Calendar</div>';
			html+='<i class="clearfix"></i>';
		html+='</div>';
		html+='<div class="right-side">';
			html+='<div class="notes-icon btn-notes-checkuplog"></div>';
			html+='	<i class="clearfix"></i>';
			html+='<div class="notes-text btn-notes-checkuplog">Notes</div>';
				html+='	<i class="clearfix"></i>';
		html+='</div>';
		html+='<div class="wrapper-checkuplog-label">';
			html+='	<span></span>';
		html+='</div>';
	html+='</div>';
	$('.box-two .title span').text("Check Up Log");
	$('.box-two-content').html(html);
	get_checkuplog_dates();
	checkuplog_bindfunc();
	switchwindow("checkuplog");
}
function checkuplog_bindfunc(){
	$('.btn-calendar-checkuplog').click(function(){
		show_calendar_checkuplog();
	});
	$('.btn-notes-checkuplog').click(function(){
		show_notes_checkuplog();
	});	
}
function show_calendar_checkuplog(){
	html="";
	html+='<div class="wrapper-calendar-checkuplog">';
		html+='<div class="wrapper-calCheckuplog">';
			html+='<div id="calCheckuplog"></div>';
		html+='</div>';
		html+='<div class="list-calendar-checkuplog-wrap img-rounded">';
			html+='<div class="list-calendar-checkuplog">Check up log list</div>';
			html+='<div class="calendar-control-wrap">';
				html+='<a class="calendar-new calendar-btn" title="New">&nbsp;</a>';
			html+='</div>';
		html+='</div>';
		html+='<div class="wrapper-notes-calendar-checkuplog img-rounded hide">';
			html+='<div class="calendar-note-box" >';
				html+='<span>Title:&nbsp;<input type="text" id="title-note-calendar-checkuplog" /></span>';
				html+='<textarea id="notes-calendar-checkuplog"></textarea>';
			html+='</div>';
			html+='<div class="calendar-note-control-wrap">';
				html+='<a class="notes-calendar-cancel notes-calendar-btn" title="Cancel">&nbsp;</a>';
				html+='<a class="notes-calendar-save notes-calendar-btn" title="Save">&nbsp;</a>';			
				html+='<a class="notes-calendar-delete notes-calendar-btn" title="Delete">&nbsp;</a>';	
			html+='</div>';
		html+='</div>';
		html+='<i class="clearfix"></i>';
	html+='</div>';
	html+='<div class="result-message-box">&nbsp;</div>';
	$('.box-two-content').html(html);
	html='<div class="back-btn-two pull-right" title="Checkup Log"><a></a></div>';
	$('.box-two .title').append(html);
	$("#calCheckuplog").datepicker({
		dateFormat: 'yy-mm-dd',
		altField: '#dateNoteDate',
		onSelect: function(dateText, inst){
			get_checkuplog_dates();
		}
	});	
	$("#calCheckuplog").click(function(){
		get_checkuplog_dates();
	});
	$('.notes-calendar-cancel').click(function(){
		$('.wrapper-notes-calendar-checkuplog').fadeOut(500,function(){
			$('.list-calendar-checkuplog-wrap').fadeIn(500,function(){
				$('#title-note-calendar-checkuplog').val('');
				$('#notes-calendar-checkuplog').val('');					
				$('#intNoteID').val('');					
			});
		});
	});		
	$('.notes-calendar-save, .notes-save').click(function (){
		save_notes();
	});
	$('.notes-delete, .notes-calendar-delete').click(function (){
		chk_del();
	});	
	$('.calendar-new').click(function(){
		$('.list-calendar-checkuplog-wrap').fadeOut(500,function(){
			$('.wrapper-notes-calendar-checkuplog').fadeIn(500,function(){
				$('#title-note-calendar-checkuplog').val('');
				$('#notes-calendar-checkuplog').val('');					
				$('#intNoteID').val('');					
			});
		});
	});	
	$('.back-btn-two').click(function(){
		checkuplog();
		$('.back-btn-two').remove();
	});
	setTimeout("get_checkuplog_dates()",500);
}

function show_notes_checkuplog(){
	html="";
	html+='<div class="wrapper-notes-checkuplog">';
		html+='<div class="left-side notes-checkuplog-wrapper">';
			html+='<div class="list-notes-checkuplog img-rounded"></div>';
		html+='</div>';
		html+='<div class="right-side">';
			html+='<div class="notes-wrap img-rounded">';
				html+='<div class="notes-title-box">';
					html+='Title:&nbsp;<input type="text" id="title-note-checkuplog" />';
					html+='<i class="clearfix"></i>';
					html+='<textarea id="notes-checkuplog"></textarea>';
				html+='</div>';
				html+='<div class="notes-control-wrap">';
					html+='<a class="notes-prev notes-btn" title="Previous">&nbsp;</a>';
					html+='<a class="notes-save notes-btn" title="Save">&nbsp;</a>';
					html+='<a class="notes-delete notes-btn" title="Delete">&nbsp;</a>';
					html+='<a class="notes-next notes-btn" title="Next">&nbsp;</a>';
				html+='</div>';			
			html+='</div>';
		html+='</div>';
		html+='<i class="clearfix"></i>';
	html+='</div>';		
	html+='<div class="result-message-box">&nbsp;</div>';	
	$('.box-two-content').html(html);
	html='<div class="back-btn-two pull-right" title="Checkup Log"><a></a></div>';
	$('.box-two .title').append(html);	
	list_checkuplog();
	$('.notes-next').click(function(){
		move_next();
	});
	$('.notes-prev').click(function(){
		move_prev();
	});	
	$('.notes-calendar-cancel').click(function(){
		$('.wrapper-notes-calendar-checkuplog').fadeOut(500,function(){
			$('.list-calendar-checkuplog-wrap').fadeIn(500,function(){
				$('#title-note-calendar-checkuplog').val('');
				$('#notes-calendar-checkuplog').val('');					
				$('#intNoteID').val('');					
			});
		});
	});		
	$('.notes-calendar-save, .notes-save').click(function (){
		save_notes();
	});
	$('.notes-delete, .notes-calendar-delete').click(function (){
		chk_del();
	});		
	$('.back-btn-two').click(function(){
		checkuplog();
		$('.back-btn-two').remove();
	});	
}

function save_notes(){
	var dateNoteDate = $('#dateNoteDate').val();
	//block = $('.calendar-checkuplog-box').css('display');
	block = $('.wrapper-calendar-checkuplog').length;
	if(block>0){
		var strNoteTitle = $('#title-note-calendar-checkuplog').val();
		var strNotes = $('#notes-calendar-checkuplog').val();
	} else{
		var strNoteTitle = $('#title-note-checkuplog').val();
		var strNotes = $('#notes-checkuplog').val();
	}
	var intNoteID = $('#intNoteID').val();
	var intUserID = $.cookie('session_code');
	xfunc = "checkuplog/save_checkuplog";
	data_sent = {
			'dateNoteDate':dateNoteDate,
			'intUserID':intUserID,
			'strNoteTitle':strNoteTitle,
			'strNotes':strNotes,
			'intID':intNoteID
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request		
		    $( ".result-message-box" ).dialog({
		    	title: "Save calendar",
		    	autoOpen: false,
		    	minHeight: 140,
				modal: true,
				draggable: false ,
				show: {
					effect: "explode",
					duration: 300
				},
				hide: {
					effect: "explode",
					duration: 300
				},	      
				buttons: {
					"Ok": function() {
						$(this).dialog("close");
					}
				}
		      });
		    $('.ui-dialog-content').text(result.message);
			$( ".result-message-box" ).dialog("open");		    
			//block = $('.calendar-checkuplog-box').css('display');
			block = $('.wrapper-calendar-checkuplog').length;
			if(block>0){
				get_checkuplog_dates();
			} else {
				list_checkuplog();
			}
			
		}
	});		
}
function list_checkuplog(){
	var intUserID = $.cookie('session_code');
	var intActive = $('#intNoteID').val();
	xfunc = "checkuplog/list_checklupog";
	data_sent = {
			'intUserID':intUserID
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request
			html='<lu>';
			html+='<li class="rowt" id="rowt"><i class="col1">Date</i><i class="col2">Title</i></li>';
			var c = 1;
			tgle_class = 'row1';
			$(result.strNoteTitle).each(function(k,v){
				if(tgle_class == 'row2'){
					tgle_class = 'row1';
				} else {
					tgle_class = 'row2';
				}
				strActive='';
				if(intActive==result.intID[k]){
					strActive = 'checkuplog-list-active';
				}
				html+='<li id="checkuplog_list_'+result.intID[k]+'" class="'+tgle_class+' '+strActive+' row-checklupog-list">';
					html+='<a onclick="get_checkuplog('+result.intID[k]+')"><i class="col1">'+result.dateNoteDate[k]+'</i><i class="col2">'+result.strNoteTitle[k]+'</i></a>';
				html+='</li>';
				c++;
			});
			html+='</lu>';
			$('.list-notes-checkuplog').html(html);
		}
	});		
}
function get_checkuplog(num){
	$('#title-note-checkuplog').val('');
	$('#notes-checkuplog').val('');	
	$('#title-note-calendar-checkuplog').val('');
	$('#notes-calendar-checkuplog').val('');	
	xfunc = "checkuplog/get_checklupog";
	data_sent = {
			'intID':num
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request
			$('.row-checklupog-list').removeClass('checkuplog-list-active');
			$('#checkuplog_list_'+num).addClass('checkuplog-list-active');
			$('#intNoteID').val(result.intID);
			block = $('.wrapper-calendar-checkuplog').length;
			
			if(block>0){
				$('#title-note-calendar-checkuplog').val(result.strNoteTitle);
				$('#notes-calendar-checkuplog').val(result.strNotes);
				$('.list-calendar-checkuplog-wrap').fadeOut(500,function(){
					$('.wrapper-notes-calendar-checkuplog').fadeIn(500,function(){
						
					});
				});					
			} else {
				$('#title-note-checkuplog').val(result.strNoteTitle);
				$('#notes-checkuplog').val(result.strNotes);
			}
			$('#dateNoteDate').val(result.dateNoteDate);
			$('.result-message-box').html('');
		}
	});		
}
function chk_del(){
    $( ".result-message-box" ).dialog({
    	title: "Delete calendar",
    	autoOpen: false,
    	minHeight: 140,
		modal: true,
		draggable: false ,
		show: {
			effect: "explode",
			duration: 300
		},
		hide: {
			effect: "explode",
			duration: 300
		},	      
		buttons: {
			"Yes": function() {
				delete_checkuplog();
				$(this).dialog('destroy').remove();
				html='<div class="result-message-box">&nbsp;</div>';
				$('.box-two-content').append(html);
			},
			"No": function() {
				$(this).dialog("close");
			}
		}
      });
    $('.ui-dialog-content').text("Are you sure?");
	$( ".result-message-box" ).dialog("open");	
}
function delete_checkuplog(){
	var intNoteID = $('#intNoteID').val();
	xfunc = "checkuplog/delete_checklupog";
	data_sent = {
			'intID':intNoteID
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request
			//console.log(true);
			//block = $('.calendar-checkuplog-box').css('display');
			block = $('.wrapper-calendar-checkuplog').length;
			if(block>0){
				get_checkuplog_dates();
				$('#title-note-calendar-checkuplog').val('');
				$('#notes-calendar-checkuplog').val('');						
			} else {
				list_checkuplog();
				$('#title-note-checkuplog').val('');
				$('#notes-checkuplog').val('');	
			}		
			$('#intNoteID').val(0);
			$('#dateNoteDate').val('');
		    $( ".result-message-box" ).dialog({
		    	title: "Delete calendar",
		    	autoOpen: false,
		    	minHeight: 140,
				modal: true,
				draggable: false ,
				show: {
					effect: "explode",
					duration: 300
				},
				hide: {
					effect: "explode",
					duration: 300
				},	      
				buttons: {
					"Ok": function() {
						$(this).dialog("close");
					}
				}
		      });
		    $('.ui-dialog-content').text(result.message);
			$( ".result-message-box" ).dialog("open");			
		}
	});		
}
function move_next(){
	var active_list = 0;
	var no_active = 1;
	var f_id = '';
	clear_note_checkuplog_box();
	$('.list-notes-checkuplog li').each(function(){
		var bolClass = $('#'+this.id).hasClass('checkuplog-list-active');

		if(active_list==1){
			var next_id = this.id;
			list_id = next_id.split('_');
			$('#checkuplog_list_'+list_id[2]).addClass('checkuplog-list-active');

			active_list = 0;
		}
		if(bolClass==true){
			$('#'+this.id).removeClass('checkuplog-list-active');
			active_list = 1;
			no_active = 0;
		}		
		if($.trim(this.id)!='rowt'){
			if($.trim(f_id)==''){
				f_id = this.id;
			}
		}
	});
	if(no_active==1){
		$('#'+f_id).addClass('checkuplog-list-active');
	}
	$('.list-notes-checkuplog li').each(function(){
		var bolClass = $('#'+this.id).hasClass('checkuplog-list-active');
		if(bolClass==true){
			var id = this.id;
			list_id = id.split('_');
			get_checkuplog(list_id[2]);
		}		
	});	
}
function move_prev(){
	var no_active = 1;
	var last_id = $('.list-notes-checkuplog li').last()[0]['attributes'][1]['value'];
	clear_note_checkuplog_box();
	$('.list-notes-checkuplog li').each(function(){
		var bolClass = $('#'+this.id).hasClass('checkuplog-list-active');
		active_last = this.id;
		
		if(bolClass==true){
			no_active = 0;
			$('#'+this.id).removeClass('checkuplog-list-active');
			rowt = $('#'+this.id).prev().hasClass('rowt');
			
			if(rowt!=true){
				$('#'+this.id).prev().addClass('checkuplog-list-active');
				var this_id = $('#'+this.id).prev()[0]['attributes'][1]['value'];
				list_id = this_id.split('_');
				get_checkuplog(list_id[2]);
			}
		}	
	});
	if(no_active==1){
		$('#'+last_id).addClass('checkuplog-list-active');
		list_id = last_id.split('_');
		get_checkuplog(list_id[2]);		
	}
	
}